package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.PcbaDevice;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceAgeHandled;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.Date;
import java.util.List;

public interface PcbaDeviceAgeHandledDao {

    /**
     * 查询所有
     *
     * @param pcbaDeviceTypeId
     * @return
     */
    @Results(id = "pcbaDeviceAgeHandledResultMap", value = {
            @Result(column = "pcba_device_id", property = "pcbaDeviceId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_name_cn", property = "pcbaDeviceNameCn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_device_name_en", property = "pcbaDeviceNameEn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "pcba_device_type_id", property = "pcbaDeviceTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_line_id", property = "pcbaLineId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_resource_id", property = "pcbaDeviceResourceId", jdbcType = JdbcType.INTEGER),
            @Result(column = "asset_number", property = "assetNumber", jdbcType = JdbcType.VARCHAR),
            @Result(column = "product_time", property = "productTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "purpose", property = "purpose", jdbcType = JdbcType.VARCHAR),

            @Result(column = "loop_handled_status", property = "loopHandledStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "age_handled_status", property = "ageHandledStatus", jdbcType = JdbcType.INTEGER),
    })
    /**
     * 查询所有有资产编号的设备
     */
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_DEVICE_AGE_HANDLED + " t1 LEFT JOIN " + DB1TableName.PCBA_LINE + " t2 on t1.pcba_line_id = t2.pcba_line_id where 1=1 " +
            " and t1.asset_number is not null and t1.asset_number != \"\" " +
            "<if test='assetNumber != null and assetNumber != \"\" '>" +
            " and t1.asset_number like CONCAT('%',#{assetNumber},'%')" +
            "</if>" +
            "<if test='handledType != null and handledType == 1 '>" +
            " and DATEDIFF(CURDATE(), t1.product_time) > #{dayOfHighLine} " +
                "<if test='handledStatus != null and handledStatus != \"\" '>" +
                " and loop_handled_status = #{handledStatus} " +
                "</if>" +
                " and loop_handled_status IS NOT null and loop_handled_status != \"\" " +
            "</if>" +
            "<if test='handledType != null and handledType == 2 '>" +
            " and DATEDIFF(CURDATE(), t1.product_time) > #{dayOfDevice} " +
                "<if test='handledStatus != null and handledStatus != \"\" '>" +
                " and age_handled_status = #{handledStatus} " +
                "</if>" +
                " and age_handled_status IS NOT null and age_handled_status != \"\" " +
            "</if>" +
            "<if test='pcbaDeviceTypeId != null and pcbaDeviceTypeId != \"\" '>" +
            " and t1.pcba_device_type_id = #{pcbaDeviceTypeId} " +
            "</if>" +
            "<if test=\"pcbaWorkShopList != null and pcbaWorkShopList.size() > 0 \">\n" +
            "                and t2.pcba_workshop_id in\n" +
            "                <foreach item=\"item\" index=\"index\" collection=\"pcbaWorkShopList\"\n" +
            "                         open=\"(\" separator=\",\" close=\")\">\n" +
            "                    #{item}\n" +
            "                </foreach>\n" +
            "            </if>\n" +
            " order by t1.create_time" +
            "<if test='begin != null and pageSize != null and pageSize != \"\" '>" +
            " limit #{begin}, #{pageSize}" +
            "</if>" +
            "</script>")
    List<PcbaDeviceAgeHandled> queryAllAssetNumberDevices(@Param("assetNumber")String assetNumber, @Param("handledType")Integer handledType, @Param("dayOfHighLine")Double dayOfHighLine, @Param("dayOfDevice")Double dayOfDevice, @Param("pcbaDeviceTypeId")Integer pcbaDeviceTypeId, @Param("pcbaWorkShopList")List<Integer> pcbaWorkShopList,@Param("handledStatus")Integer handledStatus, @Param("begin") Integer begin, @Param("pageSize") Integer pageSize);

    /**
     * 新增
     *
     * @param pcbaDevice
     * @return
     */
    @Insert("insert into " + DB1TableName.PCBA_DEVICE_AGE_HANDLED + "(" +
            "pcba_device_id," +
            "pcba_device_name_cn," +
            "pcba_device_name_en," +
            "create_time," +
            "update_time," +
            "pcba_device_type_id," +
            "sort," +
            "pcba_line_id," +
            "pcba_device_resource_id," +
            "asset_number," +
            "product_time," +
            "purpose," +
            "loop_handled_status," +
            "age_handled_status) values " +
            "(#{pcbaDevice.pcbaDeviceId}," +
            "#{pcbaDevice.pcbaDeviceNameCn}," +
            "#{pcbaDevice.pcbaDeviceNameEn}," +
            "#{pcbaDevice.createTime}," +
            "#{pcbaDevice.updateTime}," +
            "#{pcbaDevice.pcbaDeviceTypeId}," +
            "#{pcbaDevice.sort}," +
            "#{pcbaDevice.pcbaLineId}," +
            "#{pcbaDevice.pcbaDeviceResourceId}," +
            "#{pcbaDevice.assetNumber}," +
            "#{pcbaDevice.productTime}," +
            "#{pcbaDevice.purpose}," +
            "#{pcbaDevice.loopHandledStatus}," +
            "#{pcbaDevice.ageHandledStatus}" +
            ")")
    Integer addPcbaDeviceAgeHandled(@Param("pcbaDevice") PcbaDeviceAgeHandled pcbaDevice);

    /**
     * 更新
     *
     * @param pcbaDevice
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.PCBA_DEVICE_AGE_HANDLED +
            "<set>" +
            "<if test='pcbaDevice.pcbaDeviceNameCn != null and pcbaDevice.pcbaDeviceNameCn != \"\" '>" +
            " pcba_device_name_cn = #{pcbaDevice.pcbaDeviceNameCn}," +
            "</if>" +
            "<if test='pcbaDevice.pcbaDeviceNameEn != null and pcbaDevice.pcbaDeviceNameEn != \"\" '>" +
            " pcba_device_name_en = #{pcbaDevice.pcbaDeviceNameEn}," +
            "</if>" +
            "<if test='pcbaDevice.updateTime != null '>" +
            " update_time = #{pcbaDevice.updateTime}," +
            "</if>" +
            "<if test='pcbaDevice.pcbaDeviceTypeId != null and pcbaDevice.pcbaDeviceTypeId != \"\" '>" +
            " pcba_device_type_id = #{pcbaDevice.pcbaDeviceTypeId}," +
            "</if>" +
            "<if test='pcbaDevice.sort != null and pcbaDevice.sort != \"\" '>" +
            " sort = #{pcbaDevice.sort}," +
            "</if>" +
            "<if test='pcbaDevice.pcbaLineId != null and pcbaDevice.pcbaLineId != \"\" '>" +
            " pcba_line_id = #{pcbaDevice.pcbaLineId}," +
            "</if>" +
            "<if test='pcbaDevice.pcbaDeviceResourceId != null and pcbaDevice.pcbaDeviceResourceId != \"\" '>" +
            " pcba_device_resource_id = #{pcbaDevice.pcbaDeviceResourceId}," +
            "</if>" +
            "<if test='pcbaDevice.assetNumber != null and pcbaDevice.assetNumber != \"\" '>" +
            " asset_number = #{pcbaDevice.assetNumber}," +
            "</if>" +
            "<if test='pcbaDevice.productTime != null'>" +
            " product_time = #{pcbaDevice.productTime}," +
            "</if>" +
            "<if test='pcbaDevice.purpose != null '>" +
            " purpose = #{pcbaDevice.purpose}," +
            "</if>" +
            "<if test='pcbaDevice.loopHandledStatus != null '>" +
            " loop_handled_status = #{pcbaDevice.loopHandledStatus}," +
            "</if>" +
            "<if test='pcbaDevice.ageHandledStatus != null '>" +
            " age_handled_status = #{pcbaDevice.ageHandledStatus}," +
            "</if>" +
            "</set>" +
            " where pcba_device_id = #{pcbaDevice.pcbaDeviceId}" +
            "</script>")
    Integer updatePcbaDeviceAgeHandled(@Param("pcbaDevice") PcbaDeviceAgeHandled pcbaDevice);

    @ResultMap(value="pcbaDeviceAgeHandledResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_AGE_HANDLED + " where pcba_device_id = #{pcbaDeviceId}")
    PcbaDeviceAgeHandled getById(@Param("pcbaDeviceId")Integer pcbaDeviceId);
}
